package com.etc.controller;

import com.etc.vo.UserInfoVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;

@Controller
public class HelloController {

    // url ---> servlet的doGet|doPost
    // url ---> *controler的 方法上

    // URL RequestMapping
    // http://localhost:8080/hello.action?uname=tom&age=10
    @RequestMapping(value = "hello.action", method = RequestMethod.GET)
    public ModelAndView test(String uname, @RequestParam("age") int age111) {
        // 1. 参数名和请求的数据参数名一致就可以 入参
        // 2. @RequestParam("age")

        System.out.println(uname + "@@@@" + age111); // tom@@@@10
        ModelAndView mav = new ModelAndView();
        mav.setViewName("hello"); // 视图解析器 /hello.jsp
        mav.addObject("message", uname + "欢迎来到SpringMVCD"); // 制作数据model
        return mav; // 相当于请求的转发
    }
    //        public ModelAndView test1(@RequestBody UserInfoVo vo) {
    @PostMapping(value = "hello.action")
    // 自动吧参数 映射 成数据VO模型
    public ModelAndView test1(UserInfoVo vo) {
        System.out.println(vo.getUname() + "@@@@" + vo.getAge()); // tom@@@@10
        ModelAndView mav = new ModelAndView();
        mav.setViewName("hello"); // 视图解析器 /hello.jsp
        mav.addObject("message", vo.getUname() + "欢迎来到SpringMVCD， 这时post请求"); // 制作数据model
        return mav; // 相当于请求的转发
    }

    @PutMapping(value = "hello.action")
    @ResponseBody
    public HashMap test2(String uname, int age) {
        HashMap map = new HashMap();
        map.put("message", "-------------put---------------");
        return map; // 自动变json,不走视图解析器@ResponseBody
    }

    @DeleteMapping(value = "hello.action")
    @ResponseBody
    public HashMap test3(String uname) {
        HashMap map = new HashMap();
        map.put("message", "-------------DeleteMapping---------------" + uname);
        return map; // 自动变json,不走视图解析器@ResponseBody
    }

}
